﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using MvcApplication1.Models;
using MvcApplication1.Services;
using System.Web.Security;

namespace MvcApplication1.Controllers
{
    public class AdminController : ApplicationController
    {
        public IFormsAuthenticationService FormsService { get; set; }
        public IMembershipService MembershipService { get; set; }

        protected override void Initialize(RequestContext requestContext)
        {
            if (FormsService == null) { FormsService = new FormsAuthenticationService(); }
            if (MembershipService == null) { MembershipService = new AccountMembershipService(); }

            base.Initialize(requestContext);
        }

        //
        // GET: /Admin/

        public ActionResult LogOn()
        {
            return View();
        }


        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Login()
        {
            string username = "admin";
            string password = "";

            try
            {
                password = Request.Form["password"];
                using (vulnerability_dbDataContext context = new Services.vulnerability_dbDataContext())
                {
                    var result = context.user_logon(username, password);
                    if (result == 1)
                    {
                        FormsAuthentication.SetAuthCookie(username, false);
                        return RedirectToAction("Index", "Admin");
                    }
                    else
                    {
                        ViewData["message"] = "Login unsuccessful! Please check your password.";
                        return RedirectToAction("LogOn", "Admin");
                    }
                }
            }
            catch (Exception ex)
            {
            }

            return RedirectToAction("LogOn", "Admin");

        }

        public ActionResult LogOff()
        {
            FormsService.SignOut();

            return RedirectToAction("Index", "Home");
        }


        public ActionResult Index()
        {
            return View();
        }

    }
}
